package com.ocean.array;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
 */
public class RemoveDuplicatesFromSortedArray3 {

    public static void main(String[] args) {
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
//        int[] nums = {1, 1, 2};
        System.out.println(Arrays.toString(nums));
        System.out.println(new RemoveDuplicatesFromSortedArray3().removeDuplicates(nums));
        System.out.println(Arrays.toString(nums));
    }

    /**
     * 从升序的数组中 移除重复项
     *
     * @param nums
     * @return
     */
    public int removeDuplicates(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        int slow = 1;
        int fast = 1;
        while (fast < nums.length) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }
}
